package com.issoft.studentresitor.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.issoft.studentresitor.domain.dto.MajorDTO;
import com.issoft.studentresitor.domain.pojo.Major;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.query.MajorPageQuery;
import com.issoft.studentresitor.domain.vo.MajorVo;
import com.issoft.studentresitor.service.IMajorService;
import com.issoft.studentresitor.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Api(tags = "专业信息")
@RestController
@Slf4j
@RequestMapping("/admin/major")
public class MajorController {
    @Autowired
    private IMajorService majorService;
    /**
     * 专业分页查询根据专业名称、专业代码、所属学院查询
     *     private Integer majorId;
     *     private String majorName;
     *     private String college;
     *     private Integer page;
     *     private Integer pageSize;
     */
    @ApiOperation("分页查询专业信息")
    @GetMapping("/page")
    public Result queryMajorPage(@RequestParam(required = false) Integer majorId,
                                 @RequestParam(required = false) String majorName,
                                 @RequestParam(required = false) String college,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize){
        MajorPageQuery param = new MajorPageQuery();
        param.setMajorId(majorId);
        param.setMajorName(majorName);
        param.setCollege(college);
        param.setPage(page);
        param.setPageSize(pageSize);
        PageResult<MajorVo> result = majorService.queryMajorPage(param);
        return Result.success(result);
    }
    /**
     * 根据id查询专业信息
     */
    @ApiOperation("根据id查询专业信息")
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        Major major = majorService.getById(id);
        MajorVo majorVo = new MajorVo();
        BeanUtil.copyProperties(major,majorVo);
        return Result.success(majorVo);
    }
    /**
     * 批量删除专业
     */
    @ApiOperation("批量删除专业")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        boolean b = majorService.removeByIds(ids);
        return b ? Result.success() : Result.error("批量删除失败");
    }
    /**
     * 添加专业信息
     */
    @ApiOperation("添加专业信息")
    @PostMapping
    public Result add(@RequestBody MajorDTO majorDTO){
        Major major = new Major();
        BeanUtil.copyProperties(majorDTO,major);
        boolean save = majorService.save(major);
        return save ? Result.success() : Result.error("添加失败");
    }
    /**
     * 修改专业信息
     */
    @ApiOperation("修改专业信息")
    @PutMapping
    public Result update(@RequestBody Major major){
        boolean update = majorService.updateById(major);
        return update ? Result.success() : Result.error("修改失败");
    }
    /**
     * 根据id删除专业信息
     */
    @ApiOperation("根据id删除专业信息")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean delete = majorService.removeById(id);
        return delete ? Result.success() : Result.error("删除失败");
    }
    @ApiOperation("批量导出文档")
    @GetMapping("/export")
    public void export(@RequestParam("ids") List<Long> ids, HttpServletResponse response) {
        try {
            // 执行导出操作
            majorService.export(response, ids);
        } catch (Exception e) {
            log.error("导出学生信息失败", e);
            try {
                // 检查响应是否已提交
                if (!response.isCommitted()) {
                    response.reset();
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    response.setContentType("application/json;charset=utf-8");
                    response.getWriter().write("{\"code\":500,\"message\":\"导出失败:" + e.getMessage() + "\"}");
                    response.getWriter().flush();
                }
            } catch (IOException ioException) {
                log.error("导出失败后的错误处理也失败了", ioException);
            }
        }
    }
}
